import { defineStore } from 'pinia'
import axios from '@/api'
import { ref, reactive, toRefs } from 'vue'


export default defineStore('order', () => {
    // 非一手
    const apply_history = (data: any) => {
        return axios.get(`/loan/apply_history`, data)
    }
    const apply_history_detail = (id: number) => {
        return axios.get(`/loan/apply_history/${id}`)
    }
    // 一手
    const ysf_apply_history = (data: any) => {
        return axios.get(`/loan/ysf_apply_history`, data)
    }
    const ysf_apply_history_detail = (id: number) => {
        return axios.get(`/loan/ysf_apply_history/${id}`)
    }

    // 待处理一手房列表(业务员)
    const ysf_apply_wait = (data: any) => {
        return axios.get('/internal/ysf_apply_wait', data)
    }
    // 待处理一手房详情(业务员)
    const ysf_apply_wait_detail = (id: number) => {
        return axios.get(`/internal/ysf_apply_wait/${id}`)
    }
    // 贷款待处理列表(业务员)
    const apply_wait = (data: any) => {
        return axios.get(`/internal/apply_wait`, data)
    }
    // 贷款待处理详情(业务员)
    const apply_wait_detail = (id: any) => {
        return axios.get(`/internal/apply_wait/${id}`)
    }
    // 拒绝申请
    const reject = (id: any, data: any) => {
        return axios.post(`/internal/reject/${id}`, data, true)
    }
    // 同意申请
    const do_next = (id: any, data: any) => {
        return axios.post(`/internal/do_next/${id}`, data, true)
    }


    // 支付
    const pay = (data: any) => {
        return new Promise((resolve, reject) => {
            axios.post('/order/pay', data).then((res: any) => {
                res.pay_params.timeStamp = String(res.pay_params.timeStamp);
                uni.requestPayment({
                    provider: 'wxpay',
                    orderInfo: res,
                    ...res.pay_params,
                    success: (res: any) => {
                        console.log('success')
                        resolve(res)
                    },
                    fail: (err: any) => {
                        console.log(err)
                        reject(err)
                    }
                })
            }).catch((err) => {
                reject(err)
            })
        })
    }

    // 支付二维码
    const payQrcode = (data: any) => {
        return axios.post('/order/qrcode', data, false)
    }
    return {
        apply_history, apply_history_detail, ysf_apply_history, ysf_apply_history_detail,
        ysf_apply_wait,
        ysf_apply_wait_detail,
        apply_wait,
        apply_wait_detail,
        reject,
        do_next,
        pay,
        payQrcode
    }
})

